<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container" v-if="loading">
    <div class="unbind">
      <div class="loading"></div>
    </div>
  </section>
  <section class="container" v-if="!loading && !isBind">
    <nav class="tab">
      <a
        :class="{ active: hasAccount }"
        href="javascript:"
        @click="hasAccount = true"
      >
        <i class="iconfont icon-bind"></i>
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a
        href="javascript:"
        @click="hasAccount = false"
        :class="{ active: !hasAccount }"
      >
        <i class="iconfont icon-edit"></i>
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <LoginCallbackBindPhone :openId="openId" />
    </div>
    <div class="tab-content" v-else>
      <LoginCallbackBindPatch :openId="openId" />
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import LoginHeader from "@/views/login/components/LoginHeader";
import LoginFooter from "@/views/login/components/LoginFooter";
import LoginCallbackBindPhone from "@/views/login/components/LoginCallbackBindPhone";
import LoginCallbackBindPatch from "@/views/login/components/LoginCallbackPatch";
import { findAccountByQQOpenid } from "@/api/user";
import useLoginAfter from "@/hooks/useLoginAfter";
import { ref } from "vue";
export default {
  name: "LoginCallbackPage",
  components: {
    LoginFooter,
    LoginHeader,
    LoginCallbackBindPhone,
    LoginCallbackBindPatch,
  },
  setup() {
    // 定义是否有账号
    const hasAccount = ref(true);
    // 用户是否绑定小兔仙账号
    const isBind = ref(false);
    // 加载状态
    const loading = ref(false);
    const openId = ref("");
    const { loginSuccessful } = useLoginAfter();
    // 获取qq联合中的登录相关的API
    const Login = window.QC.Login;
    // 检测地址中是否存在access_token
    // 检测用户登录状态
    if (Login.check()) {
      // 更新loading状态
      loading.value = true;
      Login.getMe((openid) => {
        // 保存openId
        openId.value = openid;
        findAccountByQQOpenid({ unionId: openid })
          // 检测到账号, 走 then 回调函数, 执行登录成功之后的逻辑
          .then((data) => {
            console.log(data);
            loading.value = false;
            isBind.value = true;
            loginSuccessful(data);
          })
          .catch(() => {
            loading.value = false;
            isBind.value = false;
            console.log("无账号");
          });
      });
    }

    return {
      hasAccount,
      loading,
      isBind,
      openId,
    };
  },
};
</script>

<style scoped lang="less">
.container {
  position: relative;
  height: 730px;
  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;
    .loading {
      height: 100%;
      background: #fff url(../../../assets/images/load.gif) no-repeat center /
        100px 100px;
    }
  }
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
